<?php

namespace app\modules\mall\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;

/**
 * 闪购
 * @author XT
 *
 */
Class Flash extends ActiveRecord {
    
    public static function db() {
        return Yii::$app->mall;
    }
    
    public static function tableName() {
        return '{{%goods}}';
    }
    
    public static function skuTableName() {
        return '{{%goods_sku}}';
    }
    
    public static function query() {
        return new Query();
    }
    /**
     * 得到闪购商品列表
     */
    public function goodsList($data) {
        $time = time();
        $page = $data['page'];
        $size = $data['size'];
        $limit = ($page - 1) * $size;
        return self::query()->select('id, name, main_pic, price, real_price, unit, inventory')
            ->from(self::tableName())
            ->where(['and', ['<', 'begin_time', $time], ['>', 'end_time', $time]])
            ->andWhere(['and', ['is_delete' => 0], ['is_up_down' => 0]])
            ->offset($limit)
            ->limit($size)
            ->orderBy(['datetime' => SORT_DESC])
            ->all(self::db());
    }
    /**
     * 得到商品详情
     * @param unknown $goodsid
     */
    public function getGoodsInfo($goodsid) {
        return self::query()->select('id, sid, name, main_pic, picids, parameter, details, price, real_price, unit, inventory, sales, sales_3day, tax, source, begin_time, end_time, 
            logistics_effect, service_days, is_up_down')
            ->from(self::tableName())
            ->where(['and', ['id' => $goodsid], ['is_delete' => 0]])
            ->one(self::db());
    }
    /**
     * 得到闪购商品最大价格
     * @param unknown $goodsid
     */
    public function getGoodsMaxPrice($goodsid) {
        return self::query()->select('price')
            ->from(self::skuTableName())
            ->where(['goods_id' => $goodsid])
            ->orderBy(['price'  => SORT_DESC])
            ->scalar(self::db());
    }
    /**
     * 得到闪购商品最小价格
     * @param unknown $goodsid
     */
    public function getGoodsMinPrice($goodsid) {
        return self::query()->select('price')
        ->from(self::skuTableName())
        ->where(['goods_id' => $goodsid])
        ->orderBy(['price'  => SORT_ASC])
        ->scalar(self::db());
    }
    /**
     * 判断闪购商品是否有sku
     * @param unknown $goodsid
     */
    public function hasSku($goodsid) {
        return self::query()
            ->from(self::skuTableName())
            ->where(['goods_id'=>$goodsid])
            ->count('*', self::db());
    }
    /**
     * 得到闪购商品颜色列表
     * @param unknown $goodsid
     */
    public function getSkucolor($goodsid) {
        return self::query()->select(color)->distinct()
            ->from(self::skuTableName())
            ->where(['goods_id'=>$goodsid])
            ->all(self::db());
    }
    /**
     * 得到闪购商品规格列表
     * @param unknown $goodsid
     */
    public function getSkusize($goodsid) {
        return self::query()->select(size)->distinct()
            ->from(self::skuTableName())
            ->where(['goods_id'=>$goodsid])
            ->all(self::db());
    }
    /**
     * 得到闪购商品sku列表
     * @param unknown $goodsid
     * @todo 加入起购和限购字段
     */
    public function getSkulist($goodsid) {
        return self::query()->select('id, goodsid, picids, size, color, price, real_price, unit, inventory, min_num, max_num')
            ->from(self::skuTableName())
            ->where(['goodsid'=>$goodsid])
            ->all(self::db());
    }
    /**
     * 判断sku列表中该颜色是否存在
     * @param unknown $goodsid
     * @param unknown $param
     */
    public function existColor($goodsid, $param) {
        return self::query()
            ->from(self::skuTableName())
            ->where(['and', ['goods_id'=>$goodsid], ['color'=>$param]])
            ->count('*', self::db());
    }
    /**
     * 判断sku列表中该规格是否存在
     * @param unknown $goodsid
     * @param unknown $param
     */
    public function existSku($goodsid, $param) {
        return self::query()
        ->from(self::skuTableName())
        ->where(['and', ['goods_id'=>$goodsid], ['size'=>$param]])
        ->count('*', self::db());
    }
}